//
//  TravelPlaceViewController.m
//  NBTripe
//
//  Created by 陈曦 on 16/1/12.
//  Copyright © 2016年 Vicki. All rights reserved.
//

#import "TravelPlaceViewController.h"
#import "HeaderTitleCollectionViewCell.h"
#import "VCsearchElementModel.h"
#import "AllScenicCollectionViewCell.h"

@interface TravelPlaceViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic,strong) NSArray *nameArray;
@property (nonatomic, retain) NSArray *allDataSource;
@property (nonatomic, retain) NSArray *sightsDataSource;
@property (nonatomic, retain) NSArray *hotelDataSource;
@property (nonatomic, retain) NSArray *restaurantDataSource;
@property (nonatomic, retain) NSArray *experienceDataSource;
@property (nonatomic, retain) NSArray *mallDataSource;
@property (nonatomic, retain) UICollectionView *collectionView;

@end

@implementation TravelPlaceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getDataSource];
    [self createCollectionView];
    [self createHeaderCollectionView];
    
    // Do any additional setup after loading the view.
}

- (void)getDataSource {
    
    NSString *string = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%ld/%@/pois/all/?sort=default&start=0&latitude=%@&longitude=%@&sign=e094e348208f3a0816c92a25445ff05e", (long)self.detailedModel.type, self.detailedModel.nId, [self.detailedModel.location valueForKey:@"lat"], [self.detailedModel.location valueForKey:@"lng"]];
    
    NSString *string2 = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%ld/%@/pois/sights/?sort=default&start=0&latitude=%@&longitude=%@&sign=e094e348208f3a0816c92a25445ff05e", (long)self.detailedModel.type, self.detailedModel.nId, [self.detailedModel.location valueForKey:@"lat"], [self.detailedModel.location valueForKey:@"lng"]];
    NSString *string3 = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%ld/%@/pois/hotel/?sort=default&start=0&latitude=%@&longitude=%@&sign=e094e348208f3a0816c92a25445ff05e", (long)self.detailedModel.type, self.detailedModel.nId, [self.detailedModel.location valueForKey:@"lat"], [self.detailedModel.location valueForKey:@"lng"]];
    NSString *string4 = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%ld/%@/pois/restaurant/?sort=default&start=0&latitude=%@&longitude=%@&sign=e094e348208f3a0816c92a25445ff05e", (long)self.detailedModel.type, self.detailedModel.nId, [self.detailedModel.location valueForKey:@"lat"], [self.detailedModel.location valueForKey:@"lng"]];
    NSString *string5 = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%ld/%@/pois/experience/?sort=default&start=0&latitude=%@&longitude=%@&sign=e094e348208f3a0816c92a25445ff05e", (long)self.detailedModel.type, self.detailedModel.nId, [self.detailedModel.location valueForKey:@"lat"], [self.detailedModel.location valueForKey:@"lng"]];
    NSString *string6 = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%ld/%@/pois/mall/?sort=default&start=0&latitude=%@&longitude=%@&sign=e094e348208f3a0816c92a25445ff05e", (long)self.detailedModel.type, self.detailedModel.nId, [self.detailedModel.location valueForKey:@"lat"], [self.detailedModel.location valueForKey:@"lng"]];

    
    [XYNetWorkTool getWithUrl:string parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        if (result) {
            NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
            NSArray *array = result[@"items"];
            for (NSDictionary *dic in array) {
                VCsearchElementModel *elementModel = [[VCsearchElementModel alloc]initWithDataSource:dic];
                [mutableArray addObject:elementModel];
            }
            self.allDataSource = [NSArray arrayWithArray:mutableArray];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
        }

    } fail:nil];
    
    [XYNetWorkTool getWithUrl:string2 parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        if (result) {
            NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
            NSArray *array = result[@"items"];
            for (NSDictionary *dic in array) {
                VCsearchElementModel *elementModel = [[VCsearchElementModel alloc]initWithDataSource:dic];
                [mutableArray addObject:elementModel];
            }
            self.sightsDataSource = [NSArray arrayWithArray:mutableArray];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
        }

    } fail:nil];
    
    [XYNetWorkTool getWithUrl:string3 parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        if (result) {
            NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
            NSArray *array = result[@"items"];
            for (NSDictionary *dic in array) {
                VCsearchElementModel *elementModel = [[VCsearchElementModel alloc]initWithDataSource:dic];
                [mutableArray addObject:elementModel];
            }
            self.hotelDataSource = [NSArray arrayWithArray:mutableArray];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
        }

    } fail:nil];
    
    [XYNetWorkTool getWithUrl:string4 parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        if (result) {
            NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
            NSArray *array = result[@"items"];
            for (NSDictionary *dic in array) {
                VCsearchElementModel *elementModel = [[VCsearchElementModel alloc]initWithDataSource:dic];
                [mutableArray addObject:elementModel];
            }
            self.restaurantDataSource = [NSArray arrayWithArray:mutableArray];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
        }
    } fail:nil];
    
    [XYNetWorkTool getWithUrl:string5 parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        if (result) {
            NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
            NSArray *array = result[@"items"];
            for (NSDictionary *dic in array) {
                VCsearchElementModel *elementModel = [[VCsearchElementModel alloc]initWithDataSource:dic];
                [mutableArray addObject:elementModel];
            }
            self.experienceDataSource = [NSArray arrayWithArray:mutableArray];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
        }
    } fail:nil];
    [XYNetWorkTool getWithUrl:string6 parameter:nil httpHeader:nil responseType:ResponseTypeJSON success:^(id result) {
        if (result) {
            NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:0];
            NSArray *array = result[@"items"];
            for (NSDictionary *dic in array) {
                VCsearchElementModel *elementModel = [[VCsearchElementModel alloc]initWithDataSource:dic];
                [mutableArray addObject:elementModel];
            }
            self.mallDataSource = [NSArray arrayWithArray:mutableArray];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.collectionView reloadData];
            });
        }
    } fail:nil];
    
    
    
}



- (void)createHeaderCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(self.view.width /7, 50);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.width , 50) collectionViewLayout:layout];
    collectionView.tag = 1001;
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.scrollEnabled = NO;
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    
    
    self.nameArray = @[@"全部", @"景点", @"住宿", @"餐厅", @"休闲", @"购物"];
    
    [collectionView registerClass:[HeaderTitleCollectionViewCell class] forCellWithReuseIdentifier:@"HeaderTitleCollectionViewCell"];
    
    [self.view addSubview:collectionView];
   
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView.tag == 1001) {
        self.collectionView.contentOffset = CGPointMake(self.collectionView.width *indexPath.item, 0);
        
        for (int i = 0; i < 6; i++) {
            NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:0];
            HeaderTitleCollectionViewCell *cell = (HeaderTitleCollectionViewCell *)[collectionView cellForItemAtIndexPath:index];
            if ([indexPath isEqual:index]) {
                
                cell.titleLabel.textColor = [UIColor colorWithRed:61/255.0 green:178/255.0 blue:193/255.0 alpha:1];
                cell.titleLabel.font = [UIFont boldSystemFontOfSize:19];
            }
            else {
                
                cell.titleLabel.textColor = [UIColor blackColor];
                cell.titleLabel.font = [UIFont boldSystemFontOfSize:16];
            }
            
            
        }
    }
    else {
        
        
    }
}



-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    UICollectionView *collection = [self.view viewWithTag:1001];
    
    for (int i = 0; i < 6; i++) {
        NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:0];
        HeaderTitleCollectionViewCell *cell = (HeaderTitleCollectionViewCell *)[collection cellForItemAtIndexPath:index];
        if ((NSInteger)(scrollView.contentOffset.x / self.view.width) == index.item) {
            
            cell.titleLabel.textColor = [UIColor colorWithRed:61/255.0 green:178/255.0 blue:193/255.0 alpha:1];
            cell.titleLabel.font = [UIFont boldSystemFontOfSize:19];
        }
        else {
            cell.titleLabel.textColor = [UIColor blackColor];
            cell.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        }
        
    }
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(self.view.width, self.view.height - 62);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.minimumLineSpacing = 0;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 50, self.view.width, self.view.height - 158) collectionViewLayout:layout];
    
    _collectionView.pagingEnabled = YES;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor colorWithRed:250/255.0 green:246/255.0 blue:232/255.0 alpha:1];
    [_collectionView registerClass:[AllScenicCollectionViewCell class] forCellWithReuseIdentifier:@"AllScenicCollectionViewCell"];
    
    [self.view addSubview:self.collectionView];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView.tag == 1001) {
        return self.nameArray.count;
    }
    else {
        return 6;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView.tag == 1001) {
        HeaderTitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HeaderTitleCollectionViewCell" forIndexPath:indexPath];
        cell.titleLabel.text = self.nameArray[indexPath.item];
        if (indexPath.item == 0) {
            cell.titleLabel.textColor = [UIColor colorWithRed:61/255.0 green:178/255.0 blue:193/255.0 alpha:1];
            cell.titleLabel.font = [UIFont boldSystemFontOfSize:19];
        }
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
               return cell;
    }
    else {
        AllScenicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AllScenicCollectionViewCell" forIndexPath:indexPath];
        if (indexPath.item == 0) {
            cell.dataSource = self.allDataSource;
        }
        else if (indexPath.item == 1) {
            cell.dataSource = self.sightsDataSource;
        }
        else if (indexPath.item == 2) {
            cell.dataSource = self.hotelDataSource;
        }
        else if (indexPath.item == 3) {
            cell.dataSource = self.restaurantDataSource;
        }
        else if (indexPath.item == 4) {
            cell.dataSource = self.experienceDataSource;
        }
        else {
            cell.dataSource = self.mallDataSource;
        }
        cell.webgg = ^(NSString *url) {
            WebViewController *web = [[WebViewController alloc] init];
            web.url = [deURL stringByAppendingString:url];
            [self.navigationController pushViewController:web animated:YES];
        };
        return cell;

        
    }
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
